package com.lphl.pet.activity;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.baoyz.swipemenulistview.SwipeMenuListView.OnMenuItemClickListener;
import com.lphl.pet.R;
import com.lphl.pet.adapter.PicListAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PetSightActivity extends Activity implements OnClickListener {

	private ImageView iv_back;
	private ImageView iv_down_icon;
	private TextView tv_share;
	private VideoView vv_video;
	private SwipeMenuListView mListView;
	public String[] allFiles;
	private PicListAdapter mAdapter;
    private List picPathList;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (mAdapter == null) {
                mAdapter = new PicListAdapter(PetSightActivity.this, picPathList);
			}
			mListView.setAdapter(mAdapter);
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_pet_sight);

		initView();
		initData();
	}

	private void initData() {
		File folder = new File(Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/");
		allFiles = folder.list();

        // 转为集合
        picPathList = new ArrayList(Arrays.asList(allFiles));
		handler.sendEmptyMessage(0);
	}

	private void initView() {
		iv_back = (ImageView) findViewById(R.id.iv_back);
		iv_down_icon = (ImageView) findViewById(R.id.iv_down_icon);
		tv_share = (TextView) findViewById(R.id.tv_share);
		vv_video = (VideoView) findViewById(R.id.vv_video);
		mListView = (SwipeMenuListView) findViewById(R.id.listView_sight);

		iv_back.setOnClickListener(this);
		iv_down_icon.setOnClickListener(this);
		tv_share.setOnClickListener(this);


		// step 1. create a MenuCreator
		SwipeMenuCreator creator = new SwipeMenuCreator() {

			@Override
			public void create(SwipeMenu menu) {
				// create "open" item
				SwipeMenuItem openItem = new SwipeMenuItem(
						getApplicationContext());
				// set item background
				openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9, 0xCE)));
				// set item width
				openItem.setWidth(dp2px(90));
				// set item title
				openItem.setTitle("分享");
				// set item title fontsize
				openItem.setTitleSize(18);
				// set item title font color
				openItem.setTitleColor(Color.WHITE);
				// add to menu
				menu.addMenuItem(openItem);

				// create "delete" item
				SwipeMenuItem deleteItem = new SwipeMenuItem(
						getApplicationContext());
				// set item background
				deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
						0x3F, 0x25)));
				// set item width
				deleteItem.setWidth(dp2px(90));
				// set a icon
				deleteItem.setIcon(R.drawable.ic_delete);
				// add to menu
				menu.addMenuItem(deleteItem);
			}
		};
		// set creator
		mListView.setMenuCreator(creator);

		// step 2. listener item click event
		mListView.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			@Override
			public void onMenuItemClick(int position, SwipeMenu menu, int index) {
				switch (index) {
				case 0:
					// open
					break;
				case 1:
                    delete(position);
                    picPathList.remove(position);
                    mAdapter.notifyDataSetChanged();
					break;
				}
			}
		});

	}

    private void delete(int position) {

        String path = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/"+picPathList.get(position);
        File delFile = new File(path);
        if(delFile.exists()){
            delFile.delete();
            Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(),"文件不存在",Toast.LENGTH_LONG).show();
        }
    }

	private void open(int position) {

	}


	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;

		case R.id.iv_down_icon:

			break;

		case R.id.tv_share:

			break;

		default:
			break;
		}
	}
}
